package com.binvshe.binvshe.opus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.entity.HomeEntity;
import com.binvshe.binvshe.mine.view.ThumbUpView;
import com.binvshe.binvshe.organization.entity.OpusListEntity;
import com.binvshe.binvshe.organization.tag.TagBean;
import com.bumptech.glide.Glide;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.customviews.ResizableImageView;
import com.pachong.android.frameworkbase.utils.CharSpanUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.binvshe.binvshe.R.id.container;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/2/3
 */

@SuppressLint("ValidFragment")
public class ImageTypeFragment extends BaseFragment implements ThumbUpView.OnThumbUp {
    @Bind(R.id.civUserHead)
    CircleImageView mCivUserHead;
    @Bind(R.id.tvUserName)
    TextView mTvUserName;
    @Bind(R.id.tvOrgName)
    TextView mTvOrgName;
    @Bind(R.id.tvOpusTitle)
    TextView mTvOpusTitle;
    @Bind(R.id.tuvCollection)
    ThumbUpView mTuvCollection;
    @Bind(R.id.tvOriginal)
    TextView mTvOriginal;
    @Bind(R.id.tvRole)
    TextView mTvRole;
    @Bind(R.id.tvTag)
    TextView mTvTag;
    @Bind(R.id.tvOpusContent)
    TextView mTvOpusContent;
    @Bind(R.id.linlytImages)
    LinearLayout mLinlytImages;
    HomeEntity mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        getTagData();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public ImageTypeFragment(HomeEntity data) {
        mData = data;
    }


    private void initView() {
        Account user = mData.getUser();
        mTvUserName.setText(user.getNickName());
        IML.load(getContext(), mCivUserHead, user.getPortrait());
        mTvOrgName.setText(user.getInClub() ? user.getClub().getName() : "");

        if (mData.isFavorite()) {

            mTuvCollection.setLike();
        } else {
            mTuvCollection.setUnlike();
        }
        mTuvCollection.setOnThumbUp(this);
        mTvOpusTitle.setText(mData.getTitle());
        mTvOpusContent.setText(mData.getContent());

        //<code>加载 image </code>
        for (OpusListEntity.ImagesEntity imgUrl : mData.getImages()) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_imageview, null);
            ResizableImageView imageView = (ResizableImageView) view.findViewById(R.id.ivContent);
//            ResizableImageView imageView = new ResizableImageView(getContext());
//            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//            imageView.setLayoutParams(params);
            Glide.with(getContext()).load(imgUrl.getUrl()).into(imageView);
            mLinlytImages.addView(view);
        }
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_opus_normal, container, false);
    }


    /**
     * 收藏
     */
    private void addFavorite() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.EVENT_FAVORITE, "article", mData.getId());
        request.post(url, new UrlParams(), new DataRequestListener(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                mTuvCollection.setLike();
                Toast.makeText(getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                LoginActivity.start(getActivity());
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 取消收藏
     */
    private void deleteFavorite() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.EVENT_FAVORITE, "article", mData.getId());
        request.delete(url, new UrlParams(), new DataRequestListener(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                mTuvCollection.setUnlike();
                Toast.makeText(getContext(), "取消收藏", Toast.LENGTH_SHORT).show();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                LoginActivity.start(getActivity());
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void like(boolean like) {
        if (like) {
            addFavorite();
        } else {
            deleteFavorite();
        }
    }


    public void getTagData() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.GET_ART_TAG, mData.getId());
        request.get(url, null, new DataRequestListener<List<TagBean>>(TagBean.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<TagBean> o) {
                super.onSuccess(o);
                String original = "";
                String role = "";
                String tag = "";
                if (o != null) {
                    for (TagBean tagBean : o) {
                        switch (tagBean.getTagType()) {
                            case 0://原创
                                original += tagBean.getTagContent() + " ";
                                break;
                            case 1://角色
                                role += tagBean.getTagContent() + " ";
                                break;
                            case 2://分类标签
                                tag += tagBean.getTagContent() + " ";
                                break;
                        }
                    }
                }
                mTvOriginal.setText(Html.fromHtml(getString(R.string.originalText, original)));
                mTvRole.setText(Html.fromHtml(getString(R.string.roleText, role)));
                mTvTag.setText(Html.fromHtml(getString(R.string.typeText, tag)));

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });

    }
}
